/*https://leetcode.cn/problems/minimum-number-of-days-to-make-m-bouquets/submissions/569222793/*/

class Solution {
public:
    int minDays(vector<int>& bloomDay, int m, int k) {
        int n = bloomDay.size();
        if(n/k < m)return -1;//此处需特别注意 不可用乘法来表示 会导致运行超时 过不了第91个样例
        
        function<bool(int)>check = [&](int d){
            int cnt = 0;//花朵的数量
            int b = 0;//花束的数量
            for(int i = 0;i < n && b < m;i++){
                if(bloomDay[i] <= d){
                    cnt++;
                    if(cnt == k){
                        b++;
                        cnt = 0;
                    }
                }
                else{
                    cnt = 0;
                }
            }
            return b >= m;
        };
        /*int l = INT_MAX,r = 0;
        for(int i = 0;i < n;i++){
            l = min(l,bloomDay[i]);
            r = max(r,bloomDay[i]);
        }*/
        int l = *min_element(bloomDay.begin(),bloomDay.end()),r = *max_element(bloomDay.begin(),bloomDay.end());
        while(l < r){
            int mid = l + (r-l)/2;
            if(check(mid)){
                r = mid;
            }
            else{
                l = mid+1;
            }
        }
        return l;
    }
};